Simplify lockfile tests
authorAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 25 Aug 2016 08:34:25 +0000 (11:34 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Thu, 25 Aug 2016 11:16:17 +0000 (14:16 +0300)
tests/cargotest/support/mod.rs
tests/generate-lockfile.rs
tests/lockfile-compat.rs

index 8ade994c9c803beee2410f4698a5c0530fa3adef..c99e8e294a00ba026c44fcb8cc8aea94f444a7a3 100644 (file)
@@ -181,6 +181,13 @@ impl ProjectBuilder {
         self
     }
 
+    pub fn read_lockfile(&self) -> String {
+        let mut buffer = String::new();
+        fs::File::open(self.root().join("Cargo.lock")).unwrap()
+            .read_to_string(&mut buffer).unwrap();
+        buffer
+    }
+
     fn rm_root(&self) {
         self.root.rm_rf()
     }
index ad4d207f76d87df38ce509770c4e66aceedc7d88..94432e4f64583d9726a5c300f0c386f7c41ccaeb 100644 (file)
@@ -28,10 +28,8 @@ fn adding_and_removing_packages() {
     assert_that(p.cargo_process("generate-lockfile"),
                 execs().with_status(0));
 
-    let lockfile = p.root().join("Cargo.lock");
     let toml = p.root().join("Cargo.toml");
-    let mut lock1 = String::new();
-    File::open(&lockfile).unwrap().read_to_string(&mut lock1).unwrap();
+    let lock1 = p.read_lockfile();
 
     // add a dep
     File::create(&toml).unwrap().write_all(br#"
@@ -45,8 +43,7 @@ fn adding_and_removing_packages() {
     "#).unwrap();
     assert_that(p.cargo("generate-lockfile"),
                 execs().with_status(0));
-    let mut lock2 = String::new();
-    File::open(&lockfile).unwrap().read_to_string(&mut lock2).unwrap();
+    let lock2 = p.read_lockfile();
     assert!(lock1 != lock2);
 
     // change the dep
@@ -58,8 +55,7 @@ fn adding_and_removing_packages() {
     "#).unwrap();
     assert_that(p.cargo("generate-lockfile"),
                 execs().with_status(0));
-    let mut lock3 = String::new();
-    File::open(&lockfile).unwrap().read_to_string(&mut lock3).unwrap();
+    let lock3 = p.read_lockfile();
     assert!(lock1 != lock3);
     assert!(lock2 != lock3);
 
@@ -73,8 +69,7 @@ fn adding_and_removing_packages() {
     "#).unwrap();
     assert_that(p.cargo("generate-lockfile"),
                 execs().with_status(0));
-    let mut lock4 = String::new();
-    File::open(&lockfile).unwrap().read_to_string(&mut lock4).unwrap();
+    let lock4 = p.read_lockfile();
     assert_eq!(lock1, lock4);
 }
 
@@ -105,25 +100,20 @@ bar = "baz"
 foo = "bar"
 "#;
     let lockfile = p.root().join("Cargo.lock");
-    {
-        let mut lock = String::new();
-        File::open(&lockfile).unwrap().read_to_string(&mut lock).unwrap();
-        let data = lock + metadata;
-        File::create(&lockfile).unwrap().write_all(data.as_bytes()).unwrap();
-    }
+    let lock = p.read_lockfile();
+    let data = lock + metadata;
+    File::create(&lockfile).unwrap().write_all(data.as_bytes()).unwrap();
 
     // Build and make sure the metadata is still there
     assert_that(p.cargo("build"),
                 execs().with_status(0));
-    let mut lock = String::new();
-    File::open(&lockfile).unwrap().read_to_string(&mut lock).unwrap();
+    let lock = p.read_lockfile();
     assert!(lock.contains(metadata.trim()), "{}", lock);
 
     // Update and make sure the metadata is still there
     assert_that(p.cargo("update"),
                 execs().with_status(0));
-    let mut lock = String::new();
-    File::open(&lockfile).unwrap().read_to_string(&mut lock).unwrap();
+    let lock = p.read_lockfile();
     assert!(lock.contains(metadata.trim()), "{}", lock);
 }
 
@@ -153,10 +143,7 @@ fn preserve_line_endings_issue_2076() {
     assert_that(p.cargo("generate-lockfile"),
                 execs().with_status(0));
 
-    let mut lock0 = String::new();
-    {
-        File::open(&lockfile).unwrap().read_to_string(&mut lock0).unwrap();
-    }
+    let lock0 = p.read_lockfile();
 
     assert!(lock0.starts_with("[root]\n"));
 
@@ -168,10 +155,7 @@ fn preserve_line_endings_issue_2076() {
     assert_that(p.cargo("generate-lockfile"),
                 execs().with_status(0));
 
-    let mut lock2 = String::new();
-    {
-        File::open(&lockfile).unwrap().read_to_string(&mut lock2).unwrap();
-    }
+    let lock2 = p.read_lockfile();
 
     assert!(lock2.starts_with("[root]\r\n"));
     assert_eq!(lock1, lock2);
index 5fa34452be7f3cb138eb61f41b5281c9ef4fefed..f2296d9027ba218730bf8e870cf9b61ba2bb5e66 100644 (file)
@@ -2,9 +2,6 @@
 extern crate cargotest;
 extern crate hamcrest;
 
-use std::fs::File;
-use std::io::prelude::*;
-
 use cargotest::support::git;
 use cargotest::support::registry::Package;
 use cargotest::support::{execs, project, lines_match};
@@ -14,19 +11,6 @@ use hamcrest::assert_that;
 fn oldest_lockfile_still_works() {
     Package::new("foo", "0.1.0").publish();
 
-    let p = project("bar")
-        .file("Cargo.toml", r#"
-            [project]
-            name = "bar"
-            version = "0.0.1"
-            authors = []
-
-            [dependencies]
-            foo = "0.1.0"
-        "#)
-        .file("src/lib.rs", "");
-    p.build();
-
     let lockfile = r#"
 [root]
 name = "bar"
@@ -40,15 +24,25 @@ name = "foo"
 version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 "#;
-    File::create(p.root().join("Cargo.lock")).unwrap()
-        .write_all(lockfile.as_bytes()).unwrap();
+
+    let p = project("bar")
+        .file("Cargo.toml", r#"
+            [project]
+            name = "bar"
+            version = "0.0.1"
+            authors = []
+
+            [dependencies]
+            foo = "0.1.0"
+        "#)
+        .file("src/lib.rs", "")
+        .file("Cargo.lock", lockfile);
+    p.build();
 
     assert_that(p.cargo("build"),
                 execs().with_status(0));
 
-    let mut lock = String::new();
-    File::open(p.root().join("Cargo.lock")).unwrap()
-        .read_to_string(&mut lock).unwrap();
+    let lock = p.read_lockfile();
     assert!(lock.starts_with(lockfile.trim()));
 }
 
@@ -66,10 +60,8 @@ fn totally_wild_checksums_works() {
             [dependencies]
             foo = "0.1.0"
         "#)
-        .file("src/lib.rs", "");
-    p.build();
-
-    File::create(p.root().join("Cargo.lock")).unwrap().write_all(br#"
+        .file("src/lib.rs", "")
+        .file("Cargo.lock", r#"
 [root]
 name = "bar"
 version = "0.0.1"
@@ -85,14 +77,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 [metadata]
 "checksum baz 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "checksum"
 "checksum foo 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "checksum"
-"#).unwrap();
+"#);
+
+    p.build();
 
     assert_that(p.cargo("build"),
                 execs().with_status(0));
 
-    let mut lock = String::new();
-    File::open(p.root().join("Cargo.lock")).unwrap()
-        .read_to_string(&mut lock).unwrap();
+    let lock = p.read_lockfile();
     assert!(lock.starts_with(r#"
 [root]
 name = "bar"
@@ -124,10 +116,8 @@ fn wrong_checksum_is_an_error() {
             [dependencies]
             foo = "0.1.0"
         "#)
-        .file("src/lib.rs", "");
-    p.build();
-
-    t!(t!(File::create(p.root().join("Cargo.lock"))).write_all(br#"
+        .file("src/lib.rs", "")
+        .file("Cargo.lock", r#"
 [root]
 name = "bar"
 version = "0.0.1"
@@ -142,7 +132,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [metadata]
 "checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "checksum"
-"#));
+"#);
+
+    p.build();
 
     assert_that(p.cargo("build"),
                 execs().with_status(101).with_stderr("\
@@ -177,10 +169,8 @@ fn unlisted_checksum_is_bad_if_we_calculate() {
             [dependencies]
             foo = "0.1.0"
         "#)
-        .file("src/lib.rs", "");
-    p.build();
-
-    t!(t!(File::create(p.root().join("Cargo.lock"))).write_all(br#"
+        .file("src/lib.rs", "")
+        .file("Cargo.lock", r#"
 [root]
 name = "bar"
 version = "0.0.1"
@@ -195,7 +185,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [metadata]
 "checksum foo 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "<none>"
-"#));
+"#);
+    p.build();
 
     assert_that(p.cargo("fetch"),
                 execs().with_status(101).with_stderr("\
@@ -238,10 +229,8 @@ fn listed_checksum_bad_if_we_cannot_compute() {
             [dependencies]
             foo = {{ git = '{}' }}
         "#, git.url()))
-        .file("src/lib.rs", "");
-    p.build();
-
-    let lockfile = format!(r#"
+        .file("src/lib.rs", "")
+        .file("Cargo.lock", &format!(r#"
 [root]
 name = "bar"
 version = "0.0.1"
@@ -256,9 +245,9 @@ source = "git+{0}"
 
 [metadata]
 "checksum foo 0.1.0 (git+{0})" = "checksum"
-"#, git.url());
-    File::create(p.root().join("Cargo.lock")).unwrap()
-        .write_all(lockfile.as_bytes()).unwrap();
+"#, git.url()));
+
+    p.build();
 
     assert_that(p.cargo("fetch"),
                 execs().with_status(101).with_stderr("\
@@ -296,9 +285,7 @@ fn current_lockfile_format() {
 
     assert_that(p.cargo("build"), execs().with_status(0));
 
-    let mut actual = String::new();
-    File::open(p.root().join("Cargo.lock")).unwrap()
-        .read_to_string(&mut actual).unwrap();
+    let actual = p.read_lockfile();
 
     let expected = "\
 [root]
@@ -327,19 +314,6 @@ source = \"registry+https://github.com/rust-lang/crates.io-index\"
 fn lockfile_without_root() {
     Package::new("foo", "0.1.0").publish();
 
-    let p = project("bar")
-        .file("Cargo.toml", r#"
-            [package]
-            name = "bar"
-            version = "0.0.1"
-            authors = []
-
-            [dependencies]
-            foo = "0.1.0"
-        "#)
-        .file("src/lib.rs", "");
-    p.build();
-
     let lockfile = r#"[[package]]
 name = "bar"
 version = "0.0.1"
@@ -352,13 +326,24 @@ name = "foo"
 version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 "#;
-    File::create(p.root().join("Cargo.lock")).unwrap()
-        .write_all(lockfile.as_bytes()).unwrap();
+
+    let p = project("bar")
+        .file("Cargo.toml", r#"
+            [package]
+            name = "bar"
+            version = "0.0.1"
+            authors = []
+
+            [dependencies]
+            foo = "0.1.0"
+        "#)
+        .file("src/lib.rs", "")
+        .file("Cargo.lock", lockfile);
+
+    p.build();
 
     assert_that(p.cargo("build"), execs().with_status(0));
 
-    let mut lock = String::new();
-    File::open(p.root().join("Cargo.lock")).unwrap()
-        .read_to_string(&mut lock).unwrap();
+    let lock = p.read_lockfile();
     assert!(lock.starts_with(lockfile.trim()));
 }